﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using System.Security.Cryptography;

namespace MangoMes.Write.API.Applications.ProductTyped
{
    public class ProductTypesCreateHandler : IRequestHandler<ProductTypesCreateCommand, ResultsDTO>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<ProductTypes> ProductTypesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productTypesRepository">物料产品分类</param>
        public ProductTypesCreateHandler(IBateRepository<ProductTypes> productTypesRepository)
        {
            ProductTypesRepository = productTypesRepository;
        }
        #endregion

        #region 物料产品分类 新增
        /// <summary>
        /// 物料产品分类 新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(ProductTypesCreateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                DapperHelper dapperHelper = new DapperHelper();
                ResultsDTO result = new ResultsDTO() { Code = 0 };

                var aa = new ProductTypes()
                {
                    ProductTypeName = request.ProductTypeName,
                    Pid = request.Pid,
                    ProductDescs = request.ProductDescs,
                    ProductZoo = request.ProductZoo,
                    ProductState = request.ProductState
                };
                //ProductTypes pro = new ProductTypes();
                //pro.ProductTypeName = request.ProductTypeName;
                //pro.Pid = request.Pid;
                //pro.ProductDescs = request.ProductDescs;
                //pro.ProductZoo = request.ProductZoo;
                //pro.ProductState = 1;

                //连接mySql
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = $"insert into producttypes values (@ProductTypeId,@ProductTypeName,@Pid,@ProductDescs,@ProductZoo,@ProductState,@productTypeTime)";

                result.Code = await ProductTypesRepository.Add(str, aa, conn);
                result.Message = "物料产品分类 新增成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
